﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace CartoLab.ShortestPath
{
    /// <summary>
    /// PlanCourse 缓存从源节点到其它任一节点的最小权值路径＝》路径表
    /// </summary>
    public class PlanCourse
    {
        private Hashtable htPassedPath;

        #region ctor
        public PlanCourse(Node[] nodes, string originID)
        {
            this.htPassedPath = new Hashtable();

            Node originNode = null;
            foreach (Node node in nodes)
            {
                if (node.ID == originID)
                {
                    originNode = node;
                }
                PassedPath pPath = new PassedPath(node.ID);
                if (this.htPassedPath.ContainsKey(node.ID))
                {
                    continue;
                }
                this.htPassedPath.Add(node.ID, pPath);
                //else
                //{
                //    PassedPath pPath = new PassedPath(node.ID);
                //    this.htPassedPath.Add(node.ID, pPath);
                //}
            }

            if (originNode == null)
            {
                throw new Exception("The origin node is not exist !");
            }

            this.InitializeWeight(originNode);
        }

        private void InitializeWeight(Node originNode)
        {
            if (originNode.Edges.Length == 0)
            {
                return;
            }

            foreach (Edge edge in originNode.Edges)
            {
                PassedPath pPath = this[edge.EndNodeID];
                if (pPath == null)
                {
                    continue;
                }

                pPath.AddPassedNode(originNode.ID);
                pPath.Weight = edge.Weight;
            }
        }
        #endregion

        public PassedPath this[string nodeID]
        {
            get
            {
                return (PassedPath)this.htPassedPath[nodeID];
            }
        }
    }
}
